package com.example.flshtarvel.controller;

import com.example.flshtarvel.entity.Collect;
import com.example.flshtarvel.entity.History;
import com.example.flshtarvel.service.HistoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HistoryController {

    @Autowired
    HistoryService historyService;

    @PostMapping("/history")
    public Map<String,Object> History(History history, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        history.setUserId((String)session.getAttribute("userId"));
        int result = historyService.history(history);
        if(result==1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }

    @GetMapping("/deleteHistory")
    public Map<String,Object> deleteHistory(@RequestParam("id")String messageId, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = historyService.deleteHistory(messageId,(String)session.getAttribute("userId"));
        if(result >= 1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }

    @GetMapping("/selectHistoryByUserId")
    public Map<String,Object> selectHistoryByUserId(HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        List<History> historyList = historyService.selectHistoryByUserId((String)session.getAttribute("userId"));
        if(historyList.size()<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("resultList",historyList);
        }
        return map;
    }
}
